<template>
  <div>
    <div>
      <FormPanel
        ref="formPanel"
        :list="formList"
        :cols="3"
        :formType="type"
        labelWidth="110"
        :isSubmitBtn="false"
        @formChange="formChangeHandle"
      />
    </div>
    <div>
      <LazyLoadTab ref="lazyLoadTab" v-model="activeName" :type="handleType" :tabMenus="menus" :destroyOnClose="true" ></LazyLoadTab>
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <!-- <el-button v-if="!isEdit" @click="editActionBtn(true)" type="primary">编 辑</el-button> -->
      <!-- <el-button v-if="isEdit" @click="editActionBtn(false)">取 消</el-button> -->
      <el-button @click="closeDialog" v-if="this.type !='show'" >返 回</el-button>
      <el-button @click="saveDialog" v-if="this.type !='show'" type="primary">保 存</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryCustomerViewListDetails, updateCustomerView } from '@/api/potenCus/customer/customerinfo'
import { IDNumber, phone, email, formatDates, formatDat, } from '@/utils/validate'

import moment from 'moment'
export default {
  name: 'customerModel',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    },
    consultList: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      introducCustomerId:"",
      activeName: '',
      menus: [
        {
          title: '更多信息',
          path: 'potenCus/customer/customerinfo/tabs/moreInfo',
          params: { rowData: this.mainDetail, type: this.type },
           on: {
            myClallback: this.myClallback
          }
        },
        {
          title: '基盘信息',
          path: 'potenCus/customer/customerinfo/tabs/baseInfo',
          params: { rowData: this.mainDetail },
          on: {
            myClallback: this.closeDialog
          }
        },
        {
          title: '潜客信息',
          path: 'potenCus/customer/customerinfo/tabs/cluesInfo',
          params: { rowData: this.mainDetail },
          on: {
            myClallback: this.closeDialog2
          }
        },
        {
          title: '相关线索',
          path: 'potenCus/customer/customerinfo/tabs/aboutClues',
          params: { rowData: this.mainDetail }
        },
        {
          title: '接触记录',
          path: 'potenCus/customer/customerinfo/tabs/contactRecord',
          params: { rowData: this.mainDetail }
        },
        {
          title: '变更记录',
          path: 'potenCus/customer/customerinfo/tabs/changeRecord',
          params: { rowData: this.mainDetail }
        },
      ],
      formList: this.createFormList(),
      saveData: null,
      mainDetail: null, // 主基本信息、更多信息
      // isEdit: false,
      handleType: ''
    }
  },
  created () {
    let { rowData } = this
    this.init()
    // rowData.potentialCustomersId
  },
  methods: {
    init () {
      this.initFormData()
    },
    initFormData () {
      let { rowData } = this
      console.log('基本信息rowData', rowData, this.type)
      queryCustomerViewListDetails({'potentialCustomersId': rowData.potentialCustomersId}).then(res => {
        console.log('基本信息res', res)
        this.mainDetail = JSON.parse(JSON.stringify(res.data))
        this.activeName = '更多信息'
        this.menus.forEach(item => {
          item.params.rowData = this.mainDetail
        })
        // this.menus[0].params.rowData = this.mainDetail
        let result = res.data
        if (res.success) {
          this.formList.forEach(item => {
            if (result[item.fieldName] == null || result[item.fieldName] == undefined || result[item.fieldName] == '') {
              // do nothing
            } else {
              if (item.fieldName === 'createdAt') {
                item.initialValue = this.preDateHandler(result['createdAt'])
              } else if (item.fieldName === 'manufacturerIssueDate') {
                item.initialValue = this.preDateHandler(result['manufacturerIssueDate'])
              } else if (item.fieldName === 'consultant'){
                result['consultant'] ? item.initialValue = Number(result['consultant']) : ''
              } else {
                item.initialValue = result[item.fieldName]
              }
            }
          })
          console.log('编辑的客户类型',res.data.customerType)
          if(res.data.customerType===15231001){
            this.formList.find(item => item.fieldName ==='contactName').hidden = true
            this.formList.find(item => item.fieldName ==='gender').label = '性别'
            this.formList.find(item => item.fieldName ==='mobilePhone').label = '手机号码'
          }
          if(res.data.customerType===15231003){
            // this.contactName == this.formList1.find(item => item.fieldName ==='customerName').initialValue
            this.formList.find(item => item.fieldName ==='contactName').hidden = false
            this.formList.find(item => item.fieldName ==='contactName').initialValue = res.data.contactName
            this.formList.find(item => item.fieldName ==='gender').label = '联系人性别'
            this.formList.find(item => item.fieldName ==='mobilePhone').label = '联系人手机'
          }
        }
      })
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          rules: [{ required: true, message: '请输入客户名称', trigger: 'blur' }],
          placeholder: '请输入...',
        },
        {
          type: 'SELECT',
          label: '客户类型',
          fieldName: 'customerType',
          rules: [{ required: true, message: '请选择客户类型', trigger: 'blur' }],
          placeholder: '请选择',
          itemList: this.createDictList('1523'),
          change: val =>{
            if(val===15231001){
              this.formList.find(item => item.fieldName ==='customerType').initialValue = 15231001
              this.formList.find(item => item.fieldName ==='contactName').hidden = true
              this.formList.find(item => item.fieldName ==='gender').label = '性别'
              this.formList.find(item => item.fieldName ==='mobilePhone').label = '手机号码'
            }
            if(val===15231003){
              this.formList.find(item => item.fieldName ==='customerType').initialValue = 15231003
              this.formList.find(item => item.fieldName ==='contactName').hidden = false
              this.formList.find(item => item.fieldName ==='gender').label = '联系人性别'
              this.formList.find(item => item.fieldName ==='mobilePhone').label = '联系人手机'
            }
          },
        },
        {
          type: 'INPUT',
          label: '联系人姓名	',
          fieldName: 'contactName',
          rules: [{ required: true, message: '请输入联系人姓名', trigger: 'blur' }],
          placeholder: '请输入',
          hidden: true
        },
        {
          type: 'SELECT',
          label: '性别',
          fieldName: 'gender',
          rules: [{ required: true, message: '请选择性别', trigger: 'blur' }],
          placeholder: '请选择',
          itemList: this.createDictList('1002')
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'mobilePhone',
          placeholder: '请输入',
          maxlength: 11,
          rules: [{ required: true,  message: '请输入手机号码', trigger: 'blur' },
                  { trigger: 'blur', validator: phone }],
        },
        {
          type: 'SELECT',
          label: '所属顾问',
          fieldName: 'consultant',
          disabled: true,
          placeholder: '请选择',
          itemList: this.consultList
        },
        {
          type: 'INPUT',
          label: '备注',
          selfCols: 2,
          fieldName: 'remark',
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '创建人',
          fieldName: 'createdByName',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '创建时间',
          fieldName: 'createdAt',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'SELECT',
          label: '是否下发',
          fieldName: 'manufacturerIssue',
          disabled: true,
          placeholder: '',
          itemList: this.createDictList('1004')
        },
        {
          type: 'INPUT',
          label: '下发时间',
          fieldName: 'manufacturerIssueDate',
          disabled: true,
          placeholder: '',
        },
        {
          type: 'INPUT',
          label: '分配人',
          fieldName: 'distributionConsultantName', // distributionConsultant
          disabled: true,
          placeholder: '',
        }
      ]
    },
    saveDialog () {
      let { rowData } = this
      this.$refs.formPanel.SUBMIT_FORM()
      console.log(this.$refs.lazyLoadTab.$refs['更多信息'].$children[0].$refs,"yangy",this.$refs.lazyLoadTab.$refs['更多信息'].$children[0].$refs.moreInfo.form)
      // Has no choice, but perfect.
      let formData = JSON.parse(JSON.stringify(this.$refs.lazyLoadTab.$refs['更多信息'].$children[0].$refs.moreInfo.form))
      // console.log('我想看看转介绍客户的id', formData )
      if (this.saveData != null){
        let params = Object.assign({}, this.saveData, formData)
        params['introducCustomerId'] = this.introducCustomerId
        params['potentialCustomersId'] = rowData['potentialCustomersId']
        params['customerBusinessId'] = rowData['costomerBusinessId']
        params['birthday'] = params['birthday'] ? new Date(params['birthday']).getTime() : ''
        params['createdAt'] = params['createdAt'] ? new Date(params['createdAt']).getTime() : ''
        params['manufacturerIssueDate'] = params['manufacturerIssueDate'] ? new Date(params['manufacturerIssueDate']).getTime() : ''
        console.log(params, this.rowData)
        updateCustomerView(params).then(res => {
          if (res.success) {
            this.$message.success('保存成功')
            this.closeDialog('refresh')
          } else {
            this.$message.error(res.errMsg)
          }
        })
      }
    },
    myClallback(val){
      console.log('李呵呵',val)
      this.introducCustomerId = val?val:""

    },
    closeDialog (val) {
      this.$emit('close', val)
    },
    closeDialog2 (val) {
      this.$emit('close', val)
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    // 日期预处理
    preDateHandler(val, formatCode='YYYY-MM-DD HH:mm:ss') {
      let resultDate = val ? moment(Number(val)).format(formatCode) : val;
      if ((resultDate == 'Invalid date' || resultDate == 'Invalid Date') && val) {
        resultDate = moment(val).format(formatCode)
      }
      return resultDate;
    },
  }
}
</script>

<style lang="less">

</style>
